-- Lua Battle System - 敌人角色

local Character = require("characters.character")
local Logger = require("utils.logger")
local AI = require("ai.ai_base")

local Enemy = Character:new({
    -- 敌人特有属性
    is_controlled = false, -- 敌人角色由AI控制
    
    -- 敌人类型
    enemy_type = "normal", -- normal, elite, boss
    
    -- 掉落信息
    drops = {
        exp = 50, -- 基础经验值奖励
        gold = 20, -- 基础金币奖励
        items = {} -- 掉落物品列表，包含物品ID和掉落概率
    },
    
    -- AI相关
    ai_difficulty = "normal", -- easy, normal, hard, expert
    ai_personality = "aggressive", -- aggressive, defensive, balanced, tricky
    
    -- 战斗相关
    aggro_range = 5, -- 仇恨范围
    is_aggroed = false, -- 是否被激怒
    
    -- 敌人统计信息
    stats = {
        damage_dealt = 0,
        damage_taken = 0,
        skills_used = 0
    }
})

-- 敌人构造函数
function Enemy:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    o:init()
    return o
end

-- 初始化敌人
function Enemy:init()
    Character.init(self)
    self.enemy_type = "normal"
    self.drops = {
        exp = 50,
        gold = 20,
        items = {}
    }
    self.ai_difficulty = "normal"
    self.ai_personality = "aggressive"
    self.aggro_range = 5
    self.is_aggroed = false
    self.stats = {
        damage_dealt = 0,
        damage_taken = 0,
        skills_used = 0
    }
    
    -- 初始化AI
    self:init_ai()
    
    Logger.debug("敌人角色初始化完成: " .. self.name)
end

-- 初始化AI
function Enemy:init_ai()
    local AIConfig = {
        difficulty = self.ai_difficulty,
        personality = self.ai_personality
    }
    
    self.ai = AI:new(AIConfig)
    Logger.debug(self.name .. " AI初始化完成，难度: " .. self.ai_difficulty .. ", 性格: " .. self.ai_personality)
end

-- 设置AI难度
function Enemy:set_ai_difficulty(difficulty)
    self.ai_difficulty = difficulty
    if self.ai then
        self.ai.difficulty = difficulty
    end
    Logger.debug(self.name .. " AI难度设置为: " .. difficulty)
end

-- 设置AI性格
function Enemy:set_ai_personality(personality)
    self.ai_personality = personality
    if self.ai then
        self.ai.personality = personality
    end
    Logger.debug(self.name .. " AI性格设置为: " .. personality)
end

-- 被激怒
function Enemy:aggro()
    self.is_aggroed = true
    Logger.debug(self.name .. " 被激怒了！")
end

-- 冷静下来
function Enemy:deaggro()
    self.is_aggroed = false
    Logger.debug(self.name .. " 冷静下来了。")
end

-- 获取掉落信息
function Enemy:get_drops()
    return self.drops
end

-- 计算实际掉落物品
function Enemy:calculate_drops()
    local actual_drops = {
        exp = self.drops.exp,
        gold = self.drops.gold,
        items = {}
    }
    
    -- 根据概率计算掉落物品
    for _, drop_info in ipairs(self.drops.items) do
        if math.random() < drop_info.probability then
            table.insert(actual_drops.items, {
                id = drop_info.item_id,
                quantity = drop_info.quantity or 1
            })
        end
    end
    
    return actual_drops
end

-- 更新敌人统计信息
function Enemy:update_stats(stats)
    if stats.damage_dealt then
        self.stats.damage_dealt = self.stats.damage_dealt + stats.damage_dealt
    end
    
    if stats.damage_taken then
        self.stats.damage_taken = self.stats.damage_taken + stats.damage_taken
    end
    
    if stats.skills_used then
        self.stats.skills_used = self.stats.skills_used + stats.skills_used
    end
end

-- 执行AI决策
function Enemy:execute_ai_decision(battle_state)
    if not self.ai then
        Logger.error(self.name .. " AI未初始化")
        return nil
    end
    
    local decision = self.ai:make_decision(self, battle_state)
    return decision
end

-- 受到伤害（重写，添加激怒逻辑）
function Enemy:take_damage(damage)
    local final_damage = Character.take_damage(self, damage)
    
    -- 受到伤害时被激怒
    self:aggro()
    
    -- 更新统计信息
    self:update_stats({damage_taken = final_damage})
    
    return final_damage
end

-- 执行行动（重写，添加统计信息）
function Enemy:execute_action(action, target)
    local result = Character.execute_action(self, action, target)
    
    -- 更新统计信息
    if result and result.damage then
        self:update_stats({damage_dealt = result.damage})
    end
    
    self:update_stats({skills_used = 1})
    
    return result
end

-- 获取敌人状态（扩展）
function Enemy:get_state()
    local state = Character.get_state(self)
    state.enemy_type = self.enemy_type
    state.ai_difficulty = self.ai_difficulty
    state.ai_personality = self.ai_personality
    state.is_aggroed = self.is_aggroed
    return state
end

-- 战斗胜利（敌人被击败）
function Enemy:on_battle_defeat()
    Logger.info(self.name .. " 被击败了！")
    return self:calculate_drops()
end

-- 导出模块
return Enemy